package com.xj.api.bussiness.transferLicense.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.transferLicense.entity.TbTransferLicense;
import com.xj.api.bussiness.transferLicense.service.ITbTransferLicenseService;

import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-01-11
 */
@RestController
@RequestMapping("transferLicense")
public class TbTransferLicenseController extends BaseController {

    @Autowired
    private ITbTransferLicenseService transferLicenseService;

    @GetMapping
    @ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
    public AbstractBean transferLicenseList(@RequestParam(required = false, defaultValue = "0") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return json(transferLicenseService.selectMapsPage(new Page<TbTransferLicense>(pageNum, pageSize), new EntityWrapper<TbTransferLicense>()));
    }

    @GetMapping("/{id}.auth")
    @ApiOperation(value = "  詳情", httpMethod = "GET", notes = "  詳情")
    public AbstractBean transferLicenseContent(@PathVariable Integer id) {
        return json(transferLicenseService.searchTransferLicenseAndImg(id));
    }

    @PostMapping("save")
    @ApiOperation(value = "  新增", httpMethod = "POST", notes = " 新增")
    public AbstractBean add(TbTransferLicense tbtransferlicense) {
        tbtransferlicense.setCreateDate(new Date());
        if (!transferLicenseService.insertOrUpdate(tbtransferlicense)) {
            return error();
        }
        return success();
    }

    @DeleteMapping("{id}/delete")
    @ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required = true) Integer id) {
        if (!transferLicenseService.deleteById(id)) {
            return error();
        }
        return success();
    }


    @DeleteMapping("selectTransferLicenseByEntity")
    @ApiOperation(value = "  根据过户表中的字段查询信息", httpMethod = "POST", notes = " 根据过户表中的字段查询信息")
    public AbstractBean selectTransferLicenseByEntity(TbTransferLicense tbtransferlicense) {
        List<TbTransferLicense> list = transferLicenseService.selectTransferLicenseByEntity(tbtransferlicense);
        if (list.size() > 0) {
            return json(list);
        }
        return error();
    }

    @DeleteMapping("isHaveTransferLicenseByEntity")
    @ApiOperation(value = "  根据过户表中的字段查询信息是否存在", httpMethod = "POST", notes = " 根据过户表中的字段查询信息是否存在")
    public AbstractBean isHaveTransferLicenseByEntity(TbTransferLicense tbtransferlicense) {
        List<TbTransferLicense> list = transferLicenseService.selectTransferLicenseByEntity(tbtransferlicense);
        if (list.size() > 0) {
            return SuccessMessage("信息存在", "1");
        } else {
            return fail("信息不存在", "0");
        }
    }

}
